package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.CatalogProperty;
import cn.wolfcode.shop.api.service.ICatalogPropertyService;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CatalogPropertyController {

    @Reference
    private ICatalogPropertyService catalogPropertyService;

    @RequestMapping("/catalogProperty/edit")
    @ResponseBody
    public JsonResultVo edit(Long id,String name,Integer type){
        JsonResultVo vo = new JsonResultVo(true,"操作成功!",null);
        try {
            CatalogProperty catalogProperty = catalogPropertyService.get(id);
            catalogProperty.setName(name);
            catalogProperty.setType(type);
            catalogPropertyService.update(catalogProperty);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResultVo(false,"操作失败!",null);
        }
    }

    @RequestMapping("/catalogProperty/add")
    @ResponseBody
    public JsonResultVo add(Long id,String name,Integer type){
        JsonResultVo vo = new JsonResultVo(true,"操作成功!",null);
        try {
            catalogPropertyService.save(id,name,type);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResultVo(false,"操作失败!",null);
        }
    }

    @RequestMapping("/catalogProperty/delete/{id}")
    @ResponseBody
    public JsonResultVo delete(@PathVariable("id") Long id){
        JsonResultVo vo = new JsonResultVo(true,"操作成功!",null);
        try {
            catalogPropertyService.delete(id);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResultVo(false,"删除失败",null);
        }
    };
}
